<?php


namespace app\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;

class User extends BaseController
{
    /**
     * 朋友圈列表
     */
    public function lists(){
        $_result=Db::name('friends')
              ->alias(['friends'=>'f','user'=>'u'])
              ->join('user','u.id=f.user_id')
              ->select()
              ->toArray();
        foreach ($_result as $k=>$v){
            //拼接用户评论
            $connent=Db::name('comments')
                  ->alias(['comments'=>'c','user'=>'u'])
                  ->join('user','c.user_id=u.id')
                  ->where('c.status',0)
                  ->field('c.content,c.create_time,u.user,u.user_img')
                  ->select()
                  ->toArray();
            $_result[$k]['comments']=$connent;
            //拼接点赞列表
            $gives=Db::name('give')
                  ->alias(['give'=>'c','user'=>'u'])
                  ->join('user','c.user_id=u.id')
                  ->where('c.status',0)
                  ->field('u.user,u.user_img')
                  ->select()
                  ->toArray();
            $_result[$k]['gives']=$gives;
        }
        return json(['code'=>0,"list"=>$_result]);
    }
    /**
     * 发布朋友圈
     */
    public function release(){
        $data=Request::param();
        $_data=[
              'user_id'=>$data['user_id'],
            'content'=>$data['content'],
            'iamges'=>$data['iamges'],
            'status'=>0,
            'type'=>0,
            'create_time'=>date('Y-m-d H:i:s')
        ];
        if(Db::name('friends')->insert($_data)){
            return json(['code'=>0,'发布成功！']);
        }
        return json(['code'=>0,'发布失败！']);
    }
    /**
     * 删除朋友圈
     */
    public function delete(){
        Db::name('friends')->where('id',Request::param('id'))->update(['type'=>1,'update_time'=>date('Y-m-d H:i:s')]);
        return json(['code'=>0,'msg'=>'朋友圈删除成功！']);
    }
    /**
     * 删除评论
     */
    public function del_comment(){
        $data['id']=Request::param('id');
        $data['friends_id']=Request::param('friends_id');
        Db::name('comments')->where($data)->update(['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
        return json(['code'=>0,'msg'=>'评论删除成功！']);
    }
}